﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class MarketStoreInfoController : BaseController<marketstoreinfo>
    {
        //
        // GET: /MarketStoreInfo/

        //public ActionResult Index()
        //{
        //    return View();
        //}

        public override BLL.BaseBll<marketstoreinfo> bll
        {
            get
            {
                return new MarketStoreInfoBll();
            }

        }

        public override ActionResult Get(int page, int rows, marketstoreinfo model)
        {
            //marketStoreID   fillDate  businessContactor  contactorPhone  businessDecide  decidePhone companyName
            //companyAddress  companySize  companyType  planPutTime actuallyPutTime marketState  value1
            int count;
            var list = bll.Search(model, x => true, true, x => x.marketStoreID, rows, page, out count).ToList().Select(x => new
            {
                x.marketStoreID,
                x.fillDate,
                x.businessContactor,
                x.contactorPhone,
                //x.decidePhone,  //bd 联系方式    
                x.companyName,
                x.companyAddress,
                x.companySize,//企业规模
                x.companyType,//企业性质
               // x.planPutTime,
               // x.actuallyPutTime,
                marketState = x.marketState == 1 ? "已投放" : x.marketState == 2 ? "撤架" : x.marketState ==0? "未投放（未审核通过）" : "未投放（暂未审核）",
               x.value1,//市场人员姓名 
                x.value2,//备注
            }).ToList();
            return Json(new { total = count, rows = list });
        }


        //新增点位信息
        [HttpPost]
        public override ActionResult Save(marketstoreinfo model)
        {
            string msg = "";
            int result = 0;
            int jieguo = 1;

            if (model.companyName != "" && model.companyName != null)
            {
                //判断该用户是否存在
                int a = bll.Search(x => x.contactorPhone.Equals(model.contactorPhone)).ToList().Count;
                //不存在该客户
                if (a == 0)
                {
                    if (bll.Add(model) > 0)
                    {
                        msg = "添加成功";
                        result = 1;
                        jieguo = 1;
                        return Json(new { jieguo = jieguo, state = result, msg = msg });
                    }
                    else
                    {
                        jieguo = 1;
                        msg = "添加失败";
                        return Json(new { jieguo = jieguo, state = result, msg = msg });
                    }
                }
                else
                {
                    jieguo = 0;
                    msg = "该客户已经存在，请勿重新添加";
                    return Json(new { jieguo = jieguo, state = result, msg = msg });
                }

            }
            else
            {
                jieguo = 0;
                msg = "请把信息填写完整";
                return Json(new { jieguo = jieguo, state = result, msg = msg });
            }
        }

        public override ActionResult Modify(marketstoreinfo model)
        {
            string msg = "修改失败";
            string state = "false";

            if (model.companyName != "" && model.companyAddress != "")
            {
                if (bll.Modify(model) > 0)
                {
                    msg = "修改成功";
                    state = "true";
                }
            }
            else
            {
                msg = "请填写完整";
                return Json(new { state = state, msg = msg });
            }

            return Json(new { state = state, msg = msg });
        }


        //获取选中的一条数据
        public ActionResult GetOnlyOne(long id)
        {
            
            return Json(bll.Search(x => x.marketStoreID == id).Select(x => new {                 
                x.marketStoreID,
                x.fillDate,
                x.businessContactor,
                x.contactorPhone,
                x.decidePhone,
                x.companyName,
                x.companyAddress,
                x.companySize,//企业规模
                x.companyType,//企业性质
                x.planPutTime,
                x.actuallyPutTime,
                marketStateID=x.marketState,
                marketState = x.marketState == 1 ? "已投放" : x.marketState == 2 ? "撤架" : x.marketState == 0 ? "未投放（未审核通过）" : "未投放（暂未审核）",
                x.value1,//市场人员姓名  
                x.value2,
            }).ToList()[0], JsonRequestBehavior.AllowGet);
        }

        public  ActionResult Delete(long id)
        {
            string msg = "置为无效失败";
            string state = "false";
            marketstoreinfo model=bll.Search(x=>x.marketStoreID==id).FirstOrDefault();
            model.marketState = 2;//撤架
            if (bll.Modify(model)>0)
            {
                msg = "置为无效成功";
                state = "true";
            }

            return Json(new { state = state, msg = msg });

        }

       

    }
}
